class Admin::OrdersController < Admin::BaseController
  include PaginatorHelper
  
  def index
    parameters = create_parameters(params)
    @orders = Admin.find_orders_for_admin_by_parameters(parameters)
    
    
    if(params[:page] != nil) then
      @page = params[:page].to_i
    else
      @page = 0 
    end
    hash_product_list = get_shortened_list(@orders,@page)
    @orders = hash_product_list[:object]
    @indices = hash_product_list[:indices]
  end
  
  def edit
    @order = Order.find(params[:id])
    @order_items = OrderItem.find(:all, :conditions => ['order_items.order_id = ?',@order.id])
    
    @params = params
    
    
    if(params[:page] != nil) then
      @page = params[:page].to_i
    else
      @page = 0 
    end
    hash_product_list = get_shortened_list(@order_items,@page)
    @order_items = hash_product_list[:object]
    @indices = hash_product_list[:indices]
  end
  
  def update
    if(params[:order] != nil)
      order = Order.find(params[:order][:id])
      order.update_attributes(params[:order])
      order.update_order()
      order.save
    elsif(params[:order_item] != nil)
      item = OrderItem.find(params[:order_item][:item_id])
      order = Order.find(item.order_id)
      item.shipped_quantity = params[:order_item][:shipped_quantity]
      item.save
      if(order.check_status_by_items())
        order.status = order.update_status_to_shipped()
      end
      order.save
    end
    create_parameters(params)
    redirect_to(admin_orders_path(@params))
  end
  
  private
  #creeert de parameters die worden meegegeven om de textvelden in de index ingevuld te houden.
  def create_parameters(params)
    parameters = {:search=>{:or => {'orders.status' => {:one => {:eq => ""},:two => {:eq => ""},:three => {:eq => ""},:four => {:eq => ""},:five => {:eq => ""}}},:and=>{"users.name" => {:like => ""},"orders.id" => {:eq => ""},"orders.contact_email" => {:like => ""},"orders.payment_type" => {:eq => ""},:DATE=>{'orders.created_at' =>{:gt => nil, :st => nil}}}}}
    @params = {}
    if(params == nil)
      params = {}
    end
    if(params[:user] != nil)
      parameters[:search][:and]['users.name'][:like] = params[:user]
      @params[:user] = params[:user]
      @user = params[:user]
    end
    if(params[:order_id] != nil)
      parameters[:search][:and]['orders.id'][:eq] = params[:order_id]
      @params[:order_id] = params[:order_id]
      @order_id = params[:order_id]
    end
    if(params[:email] != nil)
      parameters[:search][:and]['orders.contact_email'][:like] = params[:email]
      @params[:email] = params[:email]
      @email = params[:email]
    end
    if(params[:payment_type] != nil)
      parameters[:search][:and]['orders.payment_type'][:eq] = params[:payment_type]
      @params[:payment_type] = params[:payment_type]
      @payment_type = params[:payment_type]
    end
    if(params[:status_one] != nil)
      parameters[:search][:or]['orders.status'][:one][:eq] = params[:status_one]
      @params[:status_one] = params[:status_one]
      @status_one = params[:status_one]
    end
    if(params[:status_two] != nil)
      parameters[:search][:or]['orders.status'][:two][:eq] = params[:status_two]
      @params[:status_two] = params[:status_two]
      @status_two = params[:status_two]
    end
    if(params[:status_three] != nil)
      parameters[:search][:or]['orders.status'][:three][:eq] = params[:status_three]
      @params[:status_three] = params[:status_three]
      @status_three = params[:status_three]
    end
    if(params[:status_four] != nil)
      parameters[:search][:or]['orders.status'][:four][:eq] = params[:status_four]
      @params[:status_four] = params[:status_four]
      @status_four = params[:status_four]
    end
    if(params[:status_five] != nil)
      parameters[:search][:or]['orders.status'][:five][:eq] = params[:status_five]
      @params[:status_five] = params[:status_five]
      @status_one = params[:status_five]
    end
    if(params[:to] != nil)
      parameters[:search][:and][:DATE]['orders.created_at'][:st] = params[:to]
      date = params[:to]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_to = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:to] = params[:to]
    end
    if(params[:from] != nil)
      parameters[:search][:and][:DATE]['orders.created_at'][:gt] = params[:from]
      date = params[:from]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_from = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:from] = params[:from]
    end
    if(params[:sort] != nil)
      parameters[:sort] = params[:sort]
      @params[:sort] = params[:sort]
    end
    if(params[:sort_on] != nil)
      parameters[:sort_on] = params[:sort_on]
      @params[:sort_on] = params[:sort_on]
    end
    if(params[:page] != nil)
      @params[:page] = params[:page]
    end
    return parameters
  end
end